package util;

import android.graphics.Bitmap;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.signature.StringSignature;
import com.concerneyes.CEApp;
import com.concerneyes.R;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by Administrator on 2017/6/24 0024.
 */

public class ImageUtil {
    public static final int DEFAULT_DRAWABLE = -1;

    public static void glideloadImage(String imageUrl, final ImageView iv, int drawable) {
        if (drawable == DEFAULT_DRAWABLE) {
            drawable = R.drawable.ic_text;
        }
        Glide.with(CEApp.getInstance()).load(Uri.parse(imageUrl)).
                listener(new RequestListener<Uri, GlideDrawable>() {

                    @Override
                    public boolean onException(Exception e, Uri model, Target<GlideDrawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, Uri model, Target<GlideDrawable> target,
                                                   boolean isFromMemoryCache, boolean isFirstResource) {
                        iv.setImageDrawable(resource);
                        return false;
                    }
                })
                .signature(new StringSignature(String.valueOf(System.currentTimeMillis())))
                .placeholder(drawable)
                .into(iv);
    }

    public static void glideloadLocalImage(String imageUrl, final ImageView iv, int drawable) {
        if (drawable == DEFAULT_DRAWABLE) {
            drawable = R.drawable.ic_text;
        }
        Glide.with(CEApp.getInstance()).load(imageUrl).placeholder(drawable).into(iv);
    }

    /**
     * 保存图片对象到文件中
     *
     * @param bitmap
     * @param filePath
     * @return 成功返回true，否则返回false
     */
    public static boolean saveBitmap2File(Bitmap
                                                  bitmap, String filePath) {
        boolean result = false;
        FileOutputStream b = null;
        try {
            b = new FileOutputStream(filePath);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, b);
            result = true;
        } catch (FileNotFoundException e) {
//            LogUtil.e(new ImageUtil(), "FileNotFoundException：take photo error-->" + e.toString());
        } finally {
            try {
                b.flush();
                b.close();
            } catch (IOException e) {
//                LogUtil.e(new ImageUtil(), "IOException：take photo error-->" + e.toString());
            }
        }

        return result;
    }

}
